package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syaccp.mapper.OrderInfoMapper;
import com.syaccp.model.OrderInfo;
import com.syaccp.service.OrderDetailService;
import com.syaccp.service.OrderInfoService;
import com.syaccp.service.OrderOperRecordService;
import com.syaccp.service.TOrderDetailService;
import com.syaccp.utils.CheckWebPageUtil;
import com.syaccp.utils.DateUtils;
import com.syaccp.utils.OrderRelatedUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 针对表【t_order(订单表)】的数据库操作Service实现
 *
 * @author Lyh
 * @since 2023-07-12 11:03:55
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo>
        implements OrderInfoService {

    @Resource
    OrderInfoMapper orderInfoMapper;

    @Resource
    TOrderDetailService tOrderDetailService;

    @Resource
    OrderOperRecordService orderOperRecordService;


    @Override
    public PageInfo<OrderInfo> queryOrderInfoByPage(OrderInfo orderInfo, Integer index) {
        LambdaQueryWrapper<OrderInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtils.isNotEmpty(orderInfo.getStatus()), OrderInfo::getStatus, orderInfo.getStatus())
                .eq(ObjectUtils.isNotEmpty(orderInfo.getOrderQuality()), OrderInfo::getOrderQuality, orderInfo.getOrderQuality())
                .eq(ObjectUtils.isNotEmpty(orderInfo.getAttachmentType()), OrderInfo::getAttachmentType, orderInfo.getAttachmentType())
                .eq(ObjectUtils.isNotEmpty(orderInfo.getOrderType()), OrderInfo::getOrderType, orderInfo.getOrderType())
                .eq(ObjectUtils.isNotEmpty(orderInfo.getOrgId()), OrderInfo::getOrgId, orderInfo.getOrgId())
                .between(ObjectUtils.isNotEmpty(orderInfo.getOrderDate()), OrderInfo::getOrderDate, orderInfo.getOrderDate(), DateUtils.getTodayDateTimeLast())
                .between(ObjectUtils.isNotEmpty(orderInfo.getFinishDate()), OrderInfo::getFinishDate, orderInfo.getFinishDate(), DateUtils.getTodayDateTimeLast())
                .like(StringUtils.isNotBlank(orderInfo.getOrderTitle()), OrderInfo::getOrderTitle, orderInfo.getOrderTitle());
        PageHelper.startPage(index, 10);
        PageInfo<OrderInfo> pageInfo = new PageInfo<>(orderInfoMapper.selectList(lqw));
        List<OrderInfo> collect = pageInfo.getList().stream().peek(item -> {
            if (StringUtils.isBlank(item.getImage())) {
                item.setImage(OrderRelatedUtil.DEFAULT_PIC.getMessage());
            } else {
                if (!item.getImage().contains("127.0.0.1")) {
                    item.setImage(OrderRelatedUtil.setRealPicPath(item.getImage()));
                }
                if (!CheckWebPageUtil.checkUrlConnection(item.getImage())) {
                    item.setImage(OrderRelatedUtil.DEFAULT_PIC.getMessage());
                }
            }
            item.setOrderDetailList(tOrderDetailService.queryOrderDetailByOrderId(item.getId()));
        }).collect(Collectors.toList());
        pageInfo.setList(collect);
        return pageInfo;
    }

    @Override
    public OrderInfo queryOrderInfoByOrderId(Integer id) {
        OrderInfo orderInfo = orderInfoMapper.selectOne(Wrappers.lambdaQuery(OrderInfo.class).eq(OrderInfo::getId, id));
        if (!orderInfo.getImage().contains("127.0.0.1")) {
            orderInfo.setImage(OrderRelatedUtil.setRealPicPath(orderInfo.getImage()));
        }
        orderInfo.setOrderDetailList(tOrderDetailService.queryOrderDetailByOrderId(id));
        orderInfo.setOrderRecordList(orderOperRecordService.queryOrderRecordByOrderId(id));
        return orderInfo;
    }

    @Override
    public Integer deleteOrderInfoById(Integer orderId) {
        return orderInfoMapper.deleteById(orderId);
    }

    @Override
    public Integer insertOrderInfo(OrderInfo orderInfo) {
        return orderInfoMapper.insert(orderInfo);
    }

    @Override
    public Integer payMoney(OrderInfo orderInfo) {
        LambdaUpdateWrapper<OrderInfo> update = Wrappers.lambdaUpdate();
        update.setSql("paid_money=paid_money+" + orderInfo.getPayMoney() + ",payment=" + orderInfo.getPayment() + ",preferenti_money=" + orderInfo.getPreferentiMoney())
                .eq(OrderInfo::getId, orderInfo.getId());
        if (orderInfo.getPayment().equals(0)) {
            update.set(OrderInfo::getPaidFlag, 1).set(OrderInfo::getBalanceFlag, 2);
        } else {
            update.set(OrderInfo::getPaidFlag, 2).set(OrderInfo::getBalanceFlag, 1);
        }
        return orderInfoMapper.update(null, update);
    }

    @Override
    public Integer updateOrderAndTaskInfo(OrderInfo orderInfo) {
        int i = orderInfoMapper.updateById(orderInfo);
        orderInfo.getOrderDetailList().forEach(item -> tOrderDetailService.updateOrderDetail(item));
        return i;
    }
}




